home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.border;
-
- import com.sun.java.swing.UIManager;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Insets;
- import java.awt.Point;
- import java.awt.Rectangle;
-
- public class TitledBorder extends AbstractBorder {
- protected String title;
- protected Border border;
- protected int titlePosition;
- protected int titleJustification;
- protected Font titleFont;
- protected Color titleColor;
- public static final int DEFAULT_POSITION = 0;
- public static final int ABOVE_TOP = 1;
- public static final int TOP = 2;
- public static final int BELOW_TOP = 3;
- public static final int ABOVE_BOTTOM = 4;
- public static final int BOTTOM = 5;
- public static final int BELOW_BOTTOM = 6;
- public static final int DEFAULT_JUSTIFICATION = 0;
- public static final int LEFT = 1;
- public static final int CENTER = 2;
- public static final int RIGHT = 3;
- protected static final int EDGE_SPACING = 2;
- protected static final int TEXT_SPACING = 2;
- protected static final int TEXT_INSET_H = 5;
-
- public TitledBorder(Border border) {
- this(border, "", 1, 2, (Font)null, (Color)null);
- }
-
- public TitledBorder(Border border, String title) {
- this(border, title, 1, 2, (Font)null, (Color)null);
- }
-
- public TitledBorder(Border border, String title, int titleJustification, int titlePosition) {
- this(border, title, titleJustification, titlePosition, (Font)null, (Color)null);
- }
-
- public TitledBorder(Border border, String title, int titleJustification, int titlePosition, Font titleFont) {
- this(border, title, titleJustification, titlePosition, titleFont, (Color)null);
- }
-
- public TitledBorder(Border border, String title, int titleJustification, int titlePosition, Font titleFont, Color titleColor) {
- this.title = title;
- this.border = border;
- this.titleFont = titleFont;
- this.titleColor = titleColor;
- this.setTitleJustification(titleJustification);
- this.setTitlePosition(titlePosition);
- }
-
- public TitledBorder(String title) {
- this((Border)null, title, 1, 2, (Font)null, (Color)null);
- }
-
- public Border getBorder() {
- Border b = this.border;
- if (b == null) {
- b = UIManager.getBorder("TitledBorder.border");
- }
-
- return b;
- }
-
- public Insets getBorderInsets(Component c) {
- int descent = 0;
- int ascent = 16;
- Insets retval;
- if (this.getBorder() != null) {
- retval = this.getBorder().getBorderInsets(c);
- } else {
- retval = new Insets(0, 0, 0, 0);
- }
-
- retval.left += 4;
- retval.right += 4;
- retval.top += 4;
- retval.bottom += 4;
- if (c != null && this.getTitle() != null && !this.getTitle().equals("")) {
- Font font = this.getFont(c);
- FontMetrics fm = c.getFontMetrics(font);
- if (fm != null) {
- descent = fm.getDescent();
- ascent = fm.getAscent();
- }
-
- switch (this.getTitlePosition()) {
- case 0:
- case 2:
- retval.top += ascent + descent;
- break;
- case 1:
- retval.top += ascent + descent + (Math.max(2, 4) - 2);
- break;
- case 3:
- retval.top += ascent + descent + 2;
- break;
- case 4:
- retval.bottom += ascent + descent + 2;
- break;
- case 5:
- retval.bottom += ascent + descent;
- break;
- case 6:
- retval.bottom += ascent + 2;
- }
-
- return retval;
- } else {
- return retval;
- }
- }
-
- protected Font getFont(Component c) {
- Font font;
- if ((font = this.getTitleFont()) != null) {
- return font;
- } else {
- return c != null && (font = c.getFont()) != null ? font : new Font("Dialog", 0, 12);
- }
- }
-
- public Dimension getMinimumSize(Component c) {
- Insets insets = this.getBorderInsets(c);
- Dimension minSize = new Dimension(insets.right + insets.left, insets.top + insets.bottom);
- Font font = this.getFont(c);
- FontMetrics fm = c.getFontMetrics(font);
- switch (this.titlePosition) {
- case 0:
- case 2:
- case 3:
- case 4:
- case 5:
- default:
- minSize.width += fm.stringWidth(this.getTitle());
- break;
- case 1:
- case 6:
- minSize.width = Math.max(fm.stringWidth(this.getTitle()), minSize.width);
- }
-
- return minSize;
- }
-
- public String getTitle() {
- return this.title;
- }
-
- public Color getTitleColor() {
- Color c = this.titleColor;
- if (c == null) {
- c = UIManager.getColor("TitledBorder.titleColor");
- }
-
- return c;
- }
-
- public Font getTitleFont() {
- Font f = this.titleFont;
- if (f == null) {
- f = UIManager.getFont("TitledBorder.font");
- }
-
- return f;
- }
-
- public int getTitleJustification() {
- return this.titleJustification;
- }
-
- public int getTitlePosition() {
- return this.titlePosition;
- }
-
- public boolean isBorderOpaque() {
- return false;
- }
-
- public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
- if (this.getTitle() != null && !this.getTitle().equals("")) {
- Rectangle grooveRect = new Rectangle(2, 2, width - 4, height - 4);
- Font font = g.getFont();
- Color color = g.getColor();
- g.setFont(this.getFont(c));
- FontMetrics fm = g.getFontMetrics();
- int fontHeight = fm.getHeight();
- int descent = fm.getDescent();
- int ascent = fm.getAscent();
- Point textLoc = new Point();
- int stringWidth = fm.stringWidth(this.getTitle());
- Insets insets;
- if (this.getBorder() != null) {
- insets = this.getBorder().getBorderInsets(c);
- } else {
- insets = new Insets(0, 0, 0, 0);
- }
-
- switch (this.getTitlePosition()) {
- case 0:
- case 2:
- int var18 = Math.max(0, ascent / 2 + 2 - 2);
- grooveRect.y += var18;
- grooveRect.height -= var18;
- textLoc.y = grooveRect.y - descent + (insets.top + ascent + descent) / 2;
- break;
- case 1:
- int diff = ascent + descent + (Math.max(2, 4) - 2);
- grooveRect.y += diff;
- grooveRect.height -= diff;
- textLoc.y = grooveRect.y - (descent + 2);
- break;
- case 3:
- textLoc.y = grooveRect.y + insets.top + ascent + 2;
- break;
- case 4:
- textLoc.y = grooveRect.y + grooveRect.height - (insets.bottom + descent + 2);
- break;
- case 5:
- grooveRect.height -= fontHeight / 2;
- textLoc.y = grooveRect.y + grooveRect.height - descent + (ascent + descent - insets.bottom) / 2;
- break;
- case 6:
- grooveRect.height -= fontHeight;
- textLoc.y = grooveRect.y + grooveRect.height + ascent + 2;
- }
-
- this.getBorder().paintBorder(c, g, grooveRect.x, grooveRect.y, grooveRect.width, grooveRect.height);
- switch (this.getTitleJustification()) {
- case 0:
- case 1:
- textLoc.x = grooveRect.x + 5 + insets.left;
- break;
- case 2:
- textLoc.x = grooveRect.x + (grooveRect.width - stringWidth) / 2;
- break;
- case 3:
- textLoc.x = grooveRect.x + grooveRect.width - (stringWidth + 5 + insets.right);
- }
-
- g.setColor(c.getBackground());
- g.fillRect(textLoc.x - 2, textLoc.y - (fontHeight - descent), stringWidth + 4, fontHeight - descent);
- g.setColor(this.getTitleColor());
- g.drawString(this.getTitle(), textLoc.x, textLoc.y);
- g.setFont(font);
- g.setColor(color);
- } else {
- this.getBorder().paintBorder(c, g, x, y, width, height);
- }
- }
-
- public void setBorder(Border border) {
- this.border = border;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public void setTitleColor(Color titleColor) {
- this.titleColor = titleColor;
- }
-
- public void setTitleFont(Font titleFont) {
- this.titleFont = titleFont;
- }
-
- public void setTitleJustification(int titleJustification) {
- switch (titleJustification) {
- case 0:
- case 1:
- case 2:
- case 3:
- this.titleJustification = titleJustification;
- return;
- default:
- throw new IllegalArgumentException(titleJustification + " is not a valid title justification.");
- }
- }
-
- public void setTitlePosition(int titlePosition) {
- switch (titlePosition) {
- case 0:
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- this.titlePosition = titlePosition;
- return;
- default:
- throw new IllegalArgumentException(titlePosition + " is not a valid title position.");
- }
- }
- }
-